package com.lm.liaoming.http;


import android.content.Context;
import android.util.Log;

import com.tumblr.remember.Remember;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 韦敏敏
 */

public class AddCookiesInterceptor implements Interceptor {
    private Context context;

    public AddCookiesInterceptor(Context context) {
        super();
        this.context = context;
    }

    public AddCookiesInterceptor() {
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        final Request.Builder builder = chain.request().newBuilder();
        String cookieName = null;
        String cookie = null;
        try {
            cookieName = Remember.getString("cookie_name", "");
            cookie = Remember.getString("cookie_value", "");
        } catch (Exception e) {
            cookieName = "";
            cookie = "";
        }
        if (cookieName == null || cookieName.equals("")) {
            cookieName = "ddch.session.id";
        }
        Log.e("=======request", "Cookie:" + cookieName + "=" + cookie);
        builder.addHeader("Cookie", cookieName + "=" + cookie)
                .method(original.method(), original.body());
        return chain.proceed(builder.build());
    }
}
